﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hotel
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        CNDatabase dt = new CNDatabase();
        // Button Enter
        private void BtEnter_Click(object sender, EventArgs e)
        {
            {
                try
                {
                    if (TbID.Text == "" && TbPW.Text == "")
                    {
                        MessageBox.Show("Please input your username and password !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        TbPW.Text = "";
                        TbID.Text = "";
                        TbID.Focus();
                    }
                    else
                    {
                        string query = "SELECT * FROM Account WHERE ([IDAccount] = '" + TbID.Text + "' AND [Password]='" + TbPW.Text + "')";
                        object a = dt.xlExecuteScalar(query);
                        if (a == null)
                        {
                            MessageBox.Show("An error occurred, please try again !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                            TbPW.Text = "";
                            TbID.Text = "";
                            TbID.Focus();
                        }
                        else
                        {
                            this.Hide();
                            Profiles.UserName = TbID.Text;
                            Profiles pf = new Profiles();
                            Main.UserName = TbID.Text;
                            Main pm = new Main();
                            pm.Show();
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("An error occurred, please try again !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        //Button Cancel
        private void BtCancel_Click(object sender, EventArgs e)
        {
            //TbID.Text = "";
            //TbPW.Text = "";
            //TbID.Focus();
            DialogResult result = MessageBox.Show("Are you sure you want to exit the application?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
                Application.Exit();
        }

        //Press Enter to Login
        private void TbPW_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    if (TbID.Text == "" && TbPW.Text == "")
                    {
                        MessageBox.Show("Please input your username and password !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        TbPW.Text = "";
                        TbID.Text = "";
                        TbID.Focus();
                    }
                    else
                    {
                        string query = "SELECT * FROM Account WHERE ([IDAccount] = '" + TbID.Text + "' AND [Password]='" + TbPW.Text + "')";
                        object a = dt.xlExecuteScalar(query);
                        if (a == null)
                        {
                            MessageBox.Show("An error occurred, please try again !");
                            TbPW.Text = "";
                            TbID.Text = "";
                            TbID.Focus();
                        }
                        else
                        {
                            this.Hide();
                            //Profiles pf = new Profiles();
                            Main.UserName = TbID.Text;
                            Main pm = new Main();
                            pm.Show();
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("An error occurred, please try again !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            
        }

       
    }
}



